﻿Imports Capa_de_Acceso_a_Datos

Public Class frmCronogramaPresentacion

    Public CronogramaSeleccionadoId As Integer
    Private fechaLimite As DateTime
    Private numeroEntrega As Integer
    Private idEntrega As Integer
    Private idMes As Integer
    Private año As String
    Private inicializando As Boolean

#Region "Inicialización y Carga"

    Private Sub frmCronogramaPresentacion_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()
    End Sub

    Private Sub frmCronogramaPresentacion_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        inicializando = True
        CargarCombos()
        CargarGrillaCronograma()
        'EditarColumnasGrillaCronogramas()
        inicializando = False
    End Sub

    Private Sub CargarCombos()
        cargarComboAno()
        cargarComboMes()
    End Sub

    Private Sub cargarComboAno()

        'Combo Año 
        cboAño.ValueMember = "AnoId"
        cboAño.DisplayMember = "Ano"
        cboAño.DataSource = Constante.comboFiltroAno()
        cboAño.SelectedIndex = 0
    End Sub

    Private Sub cargarComboMes()
        'Combo Mes 
        cboMes.ValueMember = "MesId"
        cboMes.DisplayMember = "Descripcion"
        cboMes.DataSource = MesDAO.comboFiltroMes()
        cboMes.SelectedIndex = 0
    End Sub

    Private Sub ColorearGrilla(ByRef dgv As DataGridView)
        dgv.AlternatingRowsDefaultCellStyle.BackColor = Color.FloralWhite
        dgv.DefaultCellStyle.BackColor = Color.Beige
    End Sub

#End Region

#Region "Cronogramas de Presentación"

    Public Sub CargarGrillaCronograma()
        Try
            año = cboAño.SelectedValue
            idMes = cboMes.SelectedValue
            dgvCronogramaPresentacion.DataSource = CronogramaPresentacionDAO.filtroCronogramaPresentacion(año, idMes)
            dgvCronogramaPresentacion.Refresh()
            ColorearGrilla(dgvCronogramaPresentacion)
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminarCronogramaPresentacion.Click

        CronogramaSeleccionadoId = dgvCronogramaPresentacion.Item("CronogramaPresentacionId", dgvCronogramaPresentacion.CurrentRow.Index).Value
        CronogramaPresentacionDAO.eliminarCronogramaPresentacion(CronogramaSeleccionadoId)
        CargarGrillaCronograma()
        'gboAgregarEntregas.Enabled = False
        btnModificarCronogramaPresentacion.Enabled = False
        btnEliminarCronogramaPresentacion.Enabled = False
        btnVisualizarCronogramaPresentacion.Enabled = False

    End Sub

    Private Sub btnAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregarCronogramaPresentacion.Click

        frmAgregarCronogramaPresentacion.cboMes.Enabled = True
        frmAgregarCronogramaPresentacion.cboAño.Enabled = True
        frmAgregarCronogramaPresentacion.btnAgregar.Enabled = True
        frmAgregarCronogramaPresentacion.btnModificar.Enabled = False
        frmAgregarCronogramaPresentacion.LimpiarFormAgregarCronogramaPresentacion()
        frmAgregarCronogramaPresentacion.CargarCombos()
        frmAgregarCronogramaPresentacion.ShowDialog()

    End Sub

    Private Sub btnModificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModificarCronogramaPresentacion.Click
        ModificarCronograma()
    End Sub

    Private Sub btnVisualizar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVisualizarCronogramaPresentacion.Click
        If dgvCronogramaPresentacion.SelectedRows.Count > 0 Then
            CronogramaSeleccionadoId = dgvCronogramaPresentacion.Item("CronogramaPresentacionId", dgvCronogramaPresentacion.CurrentRow.Index).Value
            frmVisualizarCronogramaPresentacion.CronogramaPresentacionId = CronogramaSeleccionadoId
            frmVisualizarCronogramaPresentacion.ShowDialog()
        End If
    End Sub

    Private Sub dgvCronogramaPresentacion_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvCronogramaPresentacion.MouseDoubleClick
        ModificarCronograma()
    End Sub

    Private Sub ModificarCronograma()
        If dgvCronogramaPresentacion.SelectedRows.Count > 0 Then
            CronogramaSeleccionadoId = dgvCronogramaPresentacion.Item("CronogramaPresentacionId", dgvCronogramaPresentacion.CurrentRow.Index).Value
            frmAgregarCronogramaPresentacion.CronogramaPresentacionId = CronogramaSeleccionadoId
            frmAgregarCronogramaPresentacion.CargarCombos()
            frmAgregarCronogramaPresentacion.CargarCronograma()
            frmAgregarCronogramaPresentacion.cboMes.Enabled = False
            frmAgregarCronogramaPresentacion.cboAño.Enabled = False
            frmAgregarCronogramaPresentacion.btnAgregar.Enabled = False
            frmAgregarCronogramaPresentacion.btnModificar.Enabled = True
            frmAgregarCronogramaPresentacion.ShowDialog()
        End If
    End Sub

    Private Sub dgvCronogramaPresentacion_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvCronogramaPresentacion.SelectionChanged       
        If dgvCronogramaPresentacion.SelectedRows.Count > 0 Then
            CronogramaSeleccionadoId = dgvCronogramaPresentacion.SelectedRows(0).Cells("CronogramaPresentacionId").Value
            CargarGrillaEntregas()
            btnModificarCronogramaPresentacion.Enabled = True
            btnEliminarCronogramaPresentacion.Enabled = True
            btnVisualizarCronogramaPresentacion.Enabled = True
            gboEntregas.Enabled = True
        Else
            btnModificarCronogramaPresentacion.Enabled = False
            btnEliminarCronogramaPresentacion.Enabled = False
            btnVisualizarCronogramaPresentacion.Enabled = False
            gboEntregas.Enabled = False
        End If
    End Sub

#End Region

#Region "Entregas"

    Private Sub CargarGrillaEntregas()
        Try
            dgvEntregas.DataSource = EntregaDAO.obtenerEntregasDeCronogramaPresentacion(CronogramaSeleccionadoId)
            dgvEntregas.Refresh()
            ColorearGrilla(dgvEntregas)
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

    Private Sub btnAgregarEntrega_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregarEntrega.Click
        frmAgregarEntrega.cargarEntregaAgregar(CronogramaSeleccionadoId)
        If frmAgregarEntrega.ShowDialog = Windows.Forms.DialogResult.OK Then
            CargarGrillaEntregas()
        End If
    End Sub

    Private Sub btnModificarEntrega_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModificarEntrega.Click
        Dim idEntrega As Integer = dgvEntregas.SelectedRows(0).Cells("EntregaId").Value
        Dim fechaLimite As Date = dgvEntregas.SelectedRows(0).Cells("EntregaFechaLimite").Value
        Dim numeroEntrega As Integer = dgvEntregas.SelectedRows(0).Cells("EntregaNumero").Value
        frmAgregarEntrega.cargarEntregaModificar(numeroEntrega, fechaLimite, CronogramaSeleccionadoId, idEntrega)
        If frmAgregarEntrega.ShowDialog = Windows.Forms.DialogResult.OK Then
            CargarGrillaEntregas()
        End If

    End Sub

    Private Sub btnEliminarEntrega_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminarEntrega.Click
        idEntrega = dgvEntregas.Item("EntregaId", dgvEntregas.CurrentRow.Index).Value
        EntregaDAO.eliminarEntrega(idEntrega)
        CargarGrillaEntregas()
    End Sub

    Private Sub dgvEntregas_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvEntregas.MouseDoubleClick
        If dgvCronogramaPresentacion.SelectedRows.Count > 0 Then
            CargarDetalleEntrega()
        End If
    End Sub

    Private Sub dgvEntregas_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvEntregas.SelectionChanged
        If dgvEntregas.SelectedRows.Count > 0 Then
            btnCargarDetalle.Enabled = True
            btnEliminarEntrega.Enabled = True
            btnModificarEntrega.Enabled = True
        Else
            btnCargarDetalle.Enabled = False
            btnEliminarEntrega.Enabled = False
            btnModificarEntrega.Enabled = False
        End If
        
    End Sub

#End Region

#Region "Detalles de Entrega"

    Private Sub btnCargarDetalle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCargarDetalle.Click
        CargarDetalleEntrega()
    End Sub

    Private Sub CargarDetalleEntrega()

        fechaLimite = dgvEntregas.Item("EntregaFechaLimite", dgvEntregas.CurrentRow.Index).Value
        numeroEntrega = dgvEntregas.Item("EntregaNumero", dgvEntregas.CurrentRow.Index).Value
        idEntrega = dgvEntregas.Item("EntregaId", dgvEntregas.CurrentRow.Index).Value

        frmDetalleEntrega.idEntrega = idEntrega
        frmDetalleEntrega.idCronogramaPresentacion = CronogramaSeleccionadoId
        frmDetalleEntrega.ShowDialog()

    End Sub

#End Region

#Region "Filtros"

    Private Sub cboMes_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboMes.SelectedIndexChanged
        If Not inicializando Then
            CargarGrillaCronograma()
        End If
    End Sub

    Private Sub cboAño_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboAño.SelectedIndexChanged
        If Not inicializando Then
            CargarGrillaCronograma()
        End If
    End Sub

#End Region

    

End Class